package com.ybchen.controller;

import com.ybchen.thread.TraceIdThreadPoolExecutor;
import com.ybchen.thread.TraceRunnable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: alex
 * @create: 2024-09-22 22:35
 */
@RestController
@Slf4j
public class DemoController {
    private final static TraceIdThreadPoolExecutor THREAD_POOL_EXECUTOR = new TraceIdThreadPoolExecutor(
            5,
            5,
            60L,
            TimeUnit.SECONDS,
            new LinkedBlockingDeque<>(),
            "test-pool-",
            new java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy()
    );

    @GetMapping("hi/{msg}")
    public String hi(@PathVariable("msg") String msg) {
        log.info("我是info：{}", msg);
        log.error("我是error：{}", msg);
        log.info("当前时间戳：{}", System.currentTimeMillis());
        new Thread(new TraceRunnable(() -> {
            log.info("多线程执行---1。。。。。。" + msg);
            log.error("多线程执行----2。。。。。。" + msg);
        })).start();
        THREAD_POOL_EXECUTOR.execute(()->{
            log.info("我是线程池。。。。。。"+msg);
        });
        return "ok";
    }
}
